package com.xf.day05

object TestImmutableList {
  def main(args: Array[String]): Unit = {
    testImutableListMethod()

    // testImmutableList()
    // testCreateImmutableList()

  }

  private def testImutableListMethod(): Unit = {
    var lista = List(1, 2, 3, 4)
    val headEle = lista.head
    println(headEle) // 1

    val tailEle = lista.tail
    println(tailEle) // List(2, 3, 4)

    val headEle1 = lista.tail.head
    println(headEle1) // 2

    val reverse = lista.reverse
    println(reverse) // List(4, 3, 2, 1)

    val initEle = lista.init
    println(initEle) // List(1, 2, 3)

    val l = lista.length
    println(l) // 4

    val sorted = lista.sorted
    println(sorted) // List(1, 2, 3, 4)

    val list5 = List.range(1, 6)
    println(list5) // List(1, 2, 3, 4, 5)

    val list6 = List.range(1, 9, 2)
    println(list6) // List(1, 3, 5, 7)

    val L5 = List("Spark", "Scala", "Python", "HADOOP", "Hbase")

    val num = L5.count(s => s.length == 5)
    println(num) // 3

    val dropList = L5.drop(2)
    println(dropList) // List(Python, HADOOP, Hbase)

    val dRList = L5.dropRight(3)
    println(dRList) // List(Spark, Scala)

    val Lexists = L5.exists(s => s == "Spark")
    println(Lexists) // true

    val forList = L5.forall(s => s.endsWith("k"))
    println(forList) // false

    // 遍历出来 所有的元素
    L5.foreach(x => println(x))

    val mkList = L5.mkString("-")
    println(mkList) // Spark-Scala-Python-HADOOP-Hbase

    val filterList = L5.filterNot(s => s.length == 5)
    println(filterList) // List(Python, HADOOP)

    //  val L5 = List("Spark", "Scala", "Python" , "HADOOP" , "Hbase")
    val takeList = L5.take(3)
    println(takeList) // List(Spark, Scala, Python)

    val takeRightList = L5.takeRight(3)
    println(takeRightList) // List(Python, HADOOP, Hbase)

    val list2 = List(3, 5, 2, 1, 7)
    val ascBy = list2.sortBy(x => x)
    println(ascBy) // List(1, 2, 3, 5, 7)

    val descSortBy = list2.sortBy(x => -x)
    println(descSortBy) // List(7, 5, 3, 2, 1)

    val sxList = list2.sortWith(_ < _)
    println(sxList) // List(1, 2, 3, 5, 7)

    val jxList = list2.sortWith(_ > _)
    println(jxList) // List(7, 5, 3, 2, 1)

    //  val list2 = List(3, 5, 2, 1, 7)
    // 在列表末尾加入元素
    val addList3 = list2.:+(10)
    println(addList3) // List(3, 5, 2, 1, 7, 10)

    // 在列表开头加入元素
    val addList4 = list2.+:(1)
    println(addList4) // List(1, 3, 5, 2, 1, 7)


    val listStr = list2.toString()
    println(listStr) // List(3, 5, 2, 1, 7)
  }

  private def testCreateImmutableList(): Unit = {
    // 创建一个字符串列表
    val course: List[String] = List("Scala", "Python")

    //创建二维列表
    val dim: List[List[Int]] = List(List(0, 1), List(0, 1))

    val headEle = course.head
    println(headEle) // Scala

    val tailEle = course.tail
    println(tailEle) //  List(Python)

    //创建一个空列表
    val L = Nil
    println(L) // List()

    val L2 = List()
    println(L2) // List()

    val newList = 1 :: List(3, 5)
    val newList3 = 3 :: 4 :: 5 :: List(6, 7)
    println(newList) // List(1, 3, 5)
    println(newList3) // List(3, 4, 5, 6, 7)

    val eleList = 1 :: 3 :: 5 :: Nil
    println(eleList) // List(1, 3, 5)

    val L3 = "Spark" :: "Scala" :: "Python" :: Nil
    println(L3) // List(Spark, Scala, Python)

    val newList2 = List(1, 2) :: List(3, 4)
    println(newList2) // List(List(1, 2), 3, 4)

    val L4 = List(1, 2) ::: List(3, 4)
    println(L4) // List(1, 2, 3, 4)
  }

  private def testImmutableList(): Unit = {
    // 创建不可变集合
    val immutableSet = Set("Scala", "Python", "Java")

    val a = Set(1, 1, 2, 3)
    println(a) // Set(1, 2, 3)

    // 不要依赖 HashSet 的元素顺序，因为它是不确定的
    val bSet = Set.range(1, 6)
    println(bSet) // HashSet(5, 1, 2, 3, 4)

    val head = bSet.head
    println(head) // 5

    val tail = bSet.tail
    println(tail) // HashSet(1, 2, 3, 4)


    // Set 集合中的添加元素的方法
    val Set1 = Set("Scala", "Python")
    val Set2 = Set("C", "Java")
    val Set3 = Set1 ++ Set2
    println(Set3) // Set(Scala, Python, C, Java)

    // Set 集合中拼接集合的方法
    val Set4 = Set1.++(Set2)
    println(Set4)

    val numSet = Set(10, 60, 20, 30, 45)
    println("集合中的最小元素为:" + numSet.min) // 集合中的最小元素为:10
    println("集合中的最大元素为:" + numSet.max) // 集合中的最大元素为:60
    println("集合元素的和为:" + numSet.sum) // 集合元素的和为:165

    val ints = numSet.filter(_ % 2 == 1)
    println(ints) // HashSet(45)

    val oddSet = numSet.filter(_ % 2 == 0)
    println(oddSet) // HashSet(10, 20, 60, 30)

    val bool = numSet.forall(e => e > 100)
    println(bool) // false

    val bool1 = numSet.forall(e => e < 100)
    println(bool1) // true
    numSet.foreach(e => print(e + " ")) // 10 20 60 45 30

    println()
    val charSet = Set('a', 'b')
    val charSet1 = charSet.map(ch => Set(ch, ch.toUpper)) // Set(Set(a, A), Set(b, B))
    println(charSet1) // Set(Set(a, A), Set(b, B))

    val set = Set(1, 2, 3)
    val ints1 = set.+(4, 5)
    println(ints1) // HashSet(5, 1, 2, 3, 4)

    val ints2 = set.-(2)
    println(ints2) // Set(1, 3)

    val numSet2 = Set(10, 60, 20, 30, 45, 67)
    val Typegroup: Map[Boolean, Set[Int]] = numSet2.groupBy(x => x % 2 == 0)
    println(Typegroup) // HashMap(false -> HashSet(45, 67), true -> HashSet(10, 20, 60, 30))
  }
}
